package com.tucy.logistics.modules.system.captcha.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tucy.logistics.common.base.BaseEnum;
import com.tucy.logistics.common.constants.RedisConstants;
import com.tucy.logistics.common.entity.user.SystemCaptcha;
import com.tucy.logistics.common.result.ServiceException;
import com.tucy.logistics.modules.system.captcha.service.SystemCaptchaService;
import com.tucy.logistics.modules.system.captcha.mapper.SystemCaptchaMapper;
import com.tucy.logistics.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author tucy123
* @description 针对表【system_captcha(系统文件表)】的数据库操作Service实现
* @createDate 2023-09-06 09:34:14
*/
@Service
public class SystemCaptchaServiceImpl extends ServiceImpl<SystemCaptchaMapper, SystemCaptcha> implements SystemCaptchaService{

    @Autowired
    SystemCaptchaMapper systemCaptchaMapper;

    @Autowired
    RedisUtil redisUtil;

    @Override
    public void mathSaveCaptcha(String captcha, String uuid) {
        SystemCaptcha systemCaptcha = new SystemCaptcha();
        systemCaptcha.setUuid(uuid);
        systemCaptcha.setExpireTime(this.getDate());
        systemCaptcha.setCreateTime(new Date());
        systemCaptcha.setCommonStatus(BaseEnum.NORMAL.getValue());
        if (captcha.contains("=")){
            String retouch = this.retouch(captcha.replace(" ",""));
            systemCaptcha.setCaptcha(retouch);
        }else {
            systemCaptcha.setCaptcha(captcha);
        }

        //修改为redis，设置过期时间两分钟
        redisUtil.set(RedisConstants.logistics_captcha + uuid, systemCaptcha.getCaptcha(),120);

//        systemCaptchaMapper.mathSaveCaptcha(systemCaptcha);
//        List<SystemCaptcha> captchaList = this.lambdaQuery().between(SystemCaptcha::getExpireTime, "2023-09-06 00:00:00", new Date()).list();
//        if (ObjectUtil.isNotEmpty(captchaList)) {
//            List<String> strings = captchaList.stream().map(SystemCaptcha::getUuid).collect(Collectors.toList());
//            systemCaptchaMapper.deleteCaptcha(strings);
//        }
    }

    /**
     * 验证码有效时间
     * @return
     */
    private Date getDate(){
        Calendar cal = Calendar.getInstance();
        long currentTimeMillis = cal.getTimeInMillis();
        long tenMinutesLaterMillis = currentTimeMillis + 180000;
        cal.setTimeInMillis(tenMinutesLaterMillis);
        return cal.getTime();
    }

    /**
     * 处理验证码
     * @param captcha
     */
    private String retouch(String captcha) {
        String substr = captcha.substring(0,captcha.indexOf("="));
        BigDecimal decimal;
        try {
            if (substr.contains("+")){
                String[] split = substr.split("\\+");
                decimal = new BigDecimal(split[0]).add(new BigDecimal(split[1]));
            } else if (substr.contains("-")){
                String[] split = substr.split("\\-");
                decimal = new BigDecimal(split[0]).subtract(new BigDecimal(split[1]));
            } else if (substr.contains("*")){
                String[] split = substr.split("\\*");
                decimal = new BigDecimal(split[0]).multiply(new BigDecimal(split[1]));
            } else {
                String[] split = substr.split("\\/");
                decimal = new BigDecimal(split[0]).divide(new BigDecimal(split[1]));
            }
            return decimal.toString();
        }catch (Exception e){
            throw new ServiceException("生成验证码失败,请重试!");
        }
    }
}




